import { reactive, ref } from 'vue'

export function useForm() {
  const initialValues = {
    code: null as string | null,
    name: null as string | null,
    taskAddressFormList: [],
    tagCodeList: [],
    executorCode: null as string | null,
    executorName: null as string | null,
    startTime: null as string | null,
    deadline: null as string | null,
    specificTask: null as string | null,
    addressType: null as string | null
  }

  const state = reactive({
    detailFormRef: ref(),
    detailForm: { ...initialValues }
  })

  const meta = {
    model: state.detailForm,
    rules: {
      name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
      addressType: [{ required: true, message: '执行位置类型不能为空', trigger: 'blur' }],
      taskAddressFormList: [{ required: true, message: '执行位置不能为空', trigger: 'blur' }],
      tagCodeList: [{ required: true, message: '任务标签不能为空', trigger: 'blur' }],
      startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
      deadline: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }]
    }
  }

  const initForm = () => {
    state.detailForm = { ...initialValues }
  }

  const resetForm = () => {
    state.detailForm = { ...initialValues }
  }

  const getFormValues = () => state.detailForm

  const setDetail = (record: any) => {
    state.detailForm = { ...record }
  }

  return {
    meta,
    state,
    setDetail,
    initForm,
    resetForm,
    getFormValues
  }
}
